package com.mcxx.formulcount.gonshi;


public class CmpOperateSymbol extends OperateSymbol {
	public CmpOperateSymbol(String symb, int prio) {
		super(symb, prio);
	}

	public Object Caluc(Object pA, Object... pB) throws Exception {
		if (pA instanceof String && !(pB[0] instanceof String)) {
			throw new Exception("变量类型不一致无法进行比较运算");
		}
		if (pB[0] instanceof String && !(pA instanceof String)) {
			throw new Exception("变量类型不一致无法进行比较运算");
		}

		if (pA instanceof String) {
			int n=pA.toString().compareTo(pB[0].toString());
			if (symbol.equals("=")) {
				return n==0 ? 1 : 0;
			}
			if (symbol.equals( ">")) {
				return n>0  ? 1 : 0;
			}
			if (symbol.equals( "≥")) {
				return n>=0 ? 1 : 0;
			}
			if (symbol.equals("<")) {
				return n<0 ? 1 : 0;
			}
			if (symbol.equals("≤")) {
				return n<=0 ? 1 : 0;
			}
			if (symbol.equals( "≠")) {
				return n!=0 ? 1 : 0;
			}
		} else {
			if (symbol.equals( "=")) {
				return Double.parseDouble(pA.toString()) == Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
			if (symbol.equals(">")) {
				return Double.parseDouble(pA.toString()) > Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
			if (symbol.equals("≥")) {
				return Double.parseDouble(pA.toString()) >= Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
			if (symbol.equals("<")) {
				return Double.parseDouble(pA.toString()) < Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
			if (symbol.equals("≤")) {
				return Double.parseDouble(pA.toString()) <= Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
			if (symbol.equals("≠")) {
				return Double.parseDouble(pA.toString()) != Double.parseDouble(pB[0].toString()) ? 1 : 0;
			}
		}
		throw new Exception("不支持的运算符:" + symbol);
	}
}
